﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lime49;
using System.Resources;
using System.IO;
using LockCrypt.Core;
using LockCrypt.DAL;

namespace LockCrypt.Mobile {
    /// <summary>
    /// A form which prompts the user for a file location and password.
    /// </summary>
    public partial class FrmSetupWizard : Form {
        private int currentPage = 1;

        public FrmSetupWizard() {
            InitializeComponent();
            if(string.IsNullOrEmpty(DalConfigurationManager.Provider.FileName)) {
                txtDBLocation.Text = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "LockCrypt.lce");
            } else {
                txtDBLocation.Text = DalConfigurationManager.Provider.FileName;
            }
            itmExit.Click += (s, e) => {
                currentPage--;
                if(currentPage == 0) {
                    Application.Exit();
                } else if(currentPage == 1) {
                    itmExit.Text = I18nUtils.GetString("Strings", "Exit");
                    itmNext.Text = I18nUtils.GetString("Strings", "Next");
                    pnlPassword.BringToFront();
                } else if(currentPage == 2) {
                    itmExit.Text = I18nUtils.GetString("Strings", "Back");
                    itmNext.Text = I18nUtils.GetString("Strings", "Finish");
                    pnlDBLocation.BringToFront();
                }
            };
            itmNext.Click += (s, e) => {
                if(ValidatePage()) {
                    currentPage++;
                    switch(currentPage) {
                        case 1:
                            pnlPassword.BringToFront();
                            break;
                        case 2:
                            itmExit.Text = I18nUtils.GetString("Strings", "Back");
                            itmNext.Text = I18nUtils.GetString("Strings", "Finish");
                            pnlDBLocation.BringToFront();
                            break;
                        case 3:
                            try {
                                DalConfigurationManager.Provider.FileName = txtDBLocation.Text;
                                DalConfigurationManager.Provider.Encryption.EncryptionKey = txtPassword.Text;
                                if(!File.Exists(txtDBLocation.Text)) {
                                    DalConfigurationManager.Provider.CreateDatabase();
                                    DataCreator.AddDummyData(true);
                                }
                                var defaultSettings = WinMobileConfigurationManager.LoadConfiguration(false);
                                WinMobileConfigurationManager.SaveConfiguration(new WinMobileConfigurationManager() {
                                    FirstRun = false,
                                    Language = defaultSettings.Language,
                                    Provider = defaultSettings.Provider,
                                    LastImportPath = defaultSettings.LastImportPath
                                });
                                this.DialogResult = DialogResult.OK;
                            } catch(Exception ex) {
                                MessageBox.Show(I18nUtils.GetString("Errors", "ErrorCreatingDBFmt", ex.Message), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                            }
                            break;
                    }
                }
            };
            btnBrowse.Click += (s, e) => {
                FileInfo dbFile = null;
                if(string.IsNullOrEmpty(txtDBLocation.Text)) {
                    dbFile = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "LockCrypt.lce"));
                } else {
                    dbFile = new FileInfo(txtDBLocation.Text);
                }
                var dlg = new Lime49.Mobile.UI.OpenFileDialog(dbFile.DirectoryName, dbFile.Name);
                dlg.Filter = "*.lce";
                dlg.ShowDirectory(dbFile.DirectoryName);
                if(dlg.ShowDialog() == DialogResult.OK) {
                    txtDBLocation.Text = dlg.SelectedFile;
                }
            };
        }

        private bool ValidatePage() {
            switch(currentPage) {
                case 1:
                    if(string.IsNullOrEmpty(txtPassword.Text) && string.IsNullOrEmpty(txtConfirmPassword.Text)) {
                        if(MessageBox.Show(I18nUtils.GetString("Errors", "PassBlankDesc"), I18nUtils.GetString("Errors", "PasswordBlank"), MessageBoxButtons.YesNo, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1) == DialogResult.Yes) {
                            return true;
                        } else {
                            pnlPassword.BringToFront();
                            return false;
                        }
                    } else if(txtPassword.Text != txtConfirmPassword.Text) {
                        MessageBox.Show(I18nUtils.GetString("Errors", "PasswordsDontMatch"), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                        txtPassword.SelectAll();
                        txtPassword.Focus();
                        pnlPassword.BringToFront();
                        return false;
                    }
                    break;
                case 2:
                    if(string.IsNullOrEmpty(txtDBLocation.Text)) {
                        MessageBox.Show(I18nUtils.GetString("Errors", "PasswordsDontMatch"), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                        txtDBLocation.SelectAll();
                        txtDBLocation.Focus();
                        pnlDBLocation.BringToFront();
                        return false;
                    }/* else {
                        try {
                            //todo: check resource name!
                            MessageBox.Show(I18nUtils.GetString("Errors", "InvalidFilename"), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                        } catch {
                            txtDBLocation.SelectAll();
                            txtDBLocation.Focus();
                            pnlDBLocation.BringToFront();
                            return false;
                        }
                    }*/
                    break;
            }
            return true;
        }

    }
}